package org.opu.vcforms.datasource;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author yura
 */
public class DbQueryHandler {
    private DataReceiver dbManager;

    public DbQueryHandler(DataReceiver dbManager) {
        this.dbManager = dbManager;
    }

    /**
     *  Return all data from query
     * @param query query to database
     * @return all data from query
     */
    public Object[][] data(String query){
        if (dbManager.sendQuery(query)) {
            int rows = dbManager.getRows();
            int col = dbManager.getColumns();
            Object ret[][] = new Object[rows][col];
            for (int i = 1; i <= rows; i++) {
                for (int j = 1; j <= col; j++) {
                    ret[i - 1][j - 1] = dbManager.getObject(i, j);
                }
            }
            return ret;
        } else {
            return new Object[][]{{""}};
        }
    }

    /**
     *  Sende queryr to database using seted dbManager, and
     * choose values, that seted int prop cellection (if
     * some prop instanceof Integer, from select get column
     * with such name, else get column with specific name).
     * <br/><br/>
     * <b>Example</b>:<br/><pre>
     *
     * Suppose the database has a table:
     *      t1(int id, varchar name, varchar lastname);
     * The data is:
     *      1   Yuriy   Dunko
     *      2   Alex    Evstratyev
     *      3   Yana    Potochnyak
     *
     * Suppose we needed <b>name</b> and <b>3-d</b> field(lastname),
     * so calling method must looklike:
     *
     *      <b>get</b>({@link org.opu.vcforms.bean.db.DbManager},
     *          <font color="green">"<i>SELECT * from 't1';</i>"</font>,
     *          Arrays.<i>asList</i>(<font color="green">"name"</font>, 3));
     * </pre>
     * @param query Query thet will be sended to database
     * @param prop collumn that needed to propcesed,
     *      if it`s empty adding all data, by the collumn name.
     * @return map which consists of the {@link Map.Entry}
     * where the key is the value of from prop and value
     * is all data from answere column
     */
    public Set<Map> get(String query, Collection prop) {
        Set<Map> ret = new LinkedHashSet<Map>();

        if (dbManager.sendQuery(query)) {

            if (!(prop != null && ! prop.isEmpty())){
                prop = Arrays.asList(dbManager.getHeads());
            }

            for (int i = 1; i <= dbManager.getRows(); i++) {
                Map data = new LinkedHashMap();

                for (Object get : prop) {
                    if (get instanceof Integer) {
                        data.put(get, dbManager.getObject(i, (Integer) get));
                    } else {
                        data.put(get, dbManager.getObject(i, String.valueOf(get)));
                    }
                }

                ret.add(data);
            }
        } else {
            System.err.println(getClass().getName() + ":");
            System.err.println("Can send query - \"" + query + '"');
        }

        return ret;
    }

    /**
     *  Work the same way as {@link #get(String, java.util.Collection)},
     * with null prop.
     * @see #get(String, java.util.Collection)
     * @param query Query thet will be sended to database
     * @return
     */
    public Set<Map> get(String query) {
        return get(query, null);
    }

}
